﻿using blackhouse.WebNemo.Data;
using System;
using System.IO;

namespace blackhouse.WebNemo.Business.Groups {
    public class NemoRecordFileOperation : RecordFileOperation {

        #region Fields

        private readonly Factory factory = Factory.Instance();
        private IDataAccess data;

        #endregion

        #region Properties

        private IDataAccess DataLayer {
            get { return this.data ?? (this.data = this.factory.GetDataAccess()); }
        }

        #endregion

        #region Constructors

        public NemoRecordFileOperation(int groupId, string column, int recordId)
            : base(groupId, column, recordId) { }

        #endregion

        #region Methods

        public override bool SaveFile(RecordFileData fileData, Stream fileStream) {
            this.DataLayer.SaveGroupRecordFile(this.GroupId, this.RecordId, this.Column, fileData.FileName, fileStream, fileData.Size, this.factory.GetCurrentUser().UserId);
            return true;
        }

        public override byte[] GetFileData() {
            return this.DataLayer.GetFileStream(this.Column, this.GroupId, this.RecordId);
        }

        public override RecordFileData GetInfo() {
            var fileInfo = this.DataLayer.GetFileInfo(this.Column, this.GroupId, this.RecordId);
            var fileData = new RecordFileData();
            if (fileInfo == null || String.IsNullOrEmpty(fileInfo.FileName)) return fileData;
            fileData.Exist = true;
            fileData.FileName = fileInfo.FileName;
            fileData.Hash = fileInfo.Hash;
            fileData.Size = fileInfo.FileSize;
            return fileData;
        }

        #endregion

    }
}
